package com.graduate.cec.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.graduate.cec.common.JsonPage;
import com.graduate.cec.common.JsonResult;
import com.graduate.cec.common.ResponseCode;
import com.graduate.cec.exception.CecServiceException;
import com.graduate.cec.mapper.CategoryMapper;
import com.graduate.cec.pojo.dto.CarouselSimpleDTO;
import com.graduate.cec.pojo.dto.CategorySimpleDTO;
import com.graduate.cec.pojo.entity.Carousel;
import com.graduate.cec.pojo.entity.CarouselExample;
import com.graduate.cec.pojo.entity.Category;
import com.graduate.cec.pojo.entity.CategoryExample;
import com.graduate.cec.pojo.vo.CarouselVO;
import com.graduate.cec.pojo.vo.CategoryVO;
import com.graduate.cec.util.CopyUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class CategoryService {

    @Resource
    private CategoryMapper categoryMapper;



    /****************        增       ******************************************************************/
    public void insertCategory(Category category){
        int i = categoryMapper.insert(category);
        log.info("返回的category:{}",category);
        if (i != 1){
            throw new CecServiceException(ResponseCode.INSERT_INFO_ERROR,"新增失败!");
        }

    }

    /****************        查       ******************************************************************/
    public JsonPage<CategoryVO> getListByExample(CategorySimpleDTO categorySimpleDTO){
        PageHelper.startPage(categorySimpleDTO.getPageNum(), categorySimpleDTO.getPageSize());
        List<Category> categories = categoryMapper.selectByExample(new CategoryExample());
        List<CategoryVO> categoryVOS = CopyUtil.copyList(categories, CategoryVO.class);
        return JsonPage.restPage(new PageInfo<>(categoryVOS));
    }












    /****************        删       ******************************************************************/
    public void deleteByKeys(List<Long> id){
        CategoryExample categoryExample = new CategoryExample();
        CategoryExample.Criteria criteria = categoryExample.createCriteria();
        criteria.andIdIn(id);
        int i = categoryMapper.deleteByExample(categoryExample);
        if (i == 0){
            throw new CecServiceException(ResponseCode.DELETE_INFO_ERROR,"删除失败!");
        }
    }







    /****************        改       ******************************************************************/
    public void updateByKey(Category category){
        CategoryExample categoryExample = new CategoryExample();
        CategoryExample.Criteria criteria = categoryExample.createCriteria();
        criteria.andIdEqualTo(category.getId());
        int i = categoryMapper.updateByExampleSelective(category,categoryExample);
        if (i == 1){
            return;
        }else{
            throw new CecServiceException(ResponseCode.UPDATE_INFO_ERROR,"修改失败");
        }
    }

    /****************        查       ******************************************************************/
    public List<Category> getAllList(){
        CategoryExample categoryExample = new CategoryExample();
        List<Category> categories = categoryMapper.selectByExample(categoryExample);
        return categories;
    }

    public List<Category> getCategoryById(Long id){
        CategoryExample categoryExample = new CategoryExample();
        CategoryExample.Criteria criteria = categoryExample.createCriteria();
        criteria.andIdEqualTo(id);
        List<Category> categories = categoryMapper.selectByExample(categoryExample);
        return categories;
    }

}